package cn.exrick.xboot.modules.AppUtils.serviceimpl;

import cn.exrick.xboot.modules.AppUtils.dao.AppUtilsDao;
import cn.exrick.xboot.modules.AppUtils.entity.AppUtils;
import cn.exrick.xboot.modules.AppUtils.entity.ContentVo;
import cn.exrick.xboot.modules.AppUtils.service.AppUtilsService;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.information.entity.Information;
import cn.exrick.xboot.modules.information.service.IInformationService;
import cn.exrick.xboot.modules.news.entity.news;
import cn.exrick.xboot.modules.news.service.InewsService;
import cn.exrick.xboot.modules.subsystemNews.entity.SubsystemNews;
import cn.exrick.xboot.modules.subsystemNews.service.ISubsystemNewsService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.lang.reflect.Field;

/**
 * AppUtils接口实现
 * @author jiuqi
 */
@Slf4j
@Service
@Transactional
public class AppUtilsServiceImpl implements AppUtilsService {

    @Autowired
    private AppUtilsDao appUtilsDao;
    @Autowired
    private InewsService inewsService;
    @Autowired
    private ISubsystemNewsService iSubsystemNewsService;
    @Autowired
    private IInformationService iInformationService;
    @Override
    public AppUtilsDao getRepository() {
        return appUtilsDao;
    }

    @Override
    public Page<AppUtils> findByCondition(AppUtils appUtils, SearchVo searchVo, Pageable pageable) {

        return appUtilsDao.findAll(new Specification<AppUtils>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<AppUtils> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField=root.get("createTime");

                List<Predicate> list = new ArrayList<Predicate>();

                //创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    /**
     * 获取新闻公告等内容的接口（用于app展示）
     * 1：新闻  2公文 3：通知公告
     * @param variety
     * @return
     */
    @Override
    public ContentVo getInfoContent(int variety,String id){
        ContentVo  cv=new ContentVo();
        switch (variety) {
            case 1:
                log.info("这是新闻");
                news news = inewsService.getById(id);
                cv.setId(news.getId());
                cv.setContent(news.getNewsConten());
                cv.setTitle(news.getNewsTitle());
                break;
            case 2:
                log.info("这是公文");
                SubsystemNews subsystemNews = iSubsystemNewsService.getById(id);
                cv.setId(subsystemNews.getId());
                cv.setContent(subsystemNews.getConten());
                cv.setTitle(subsystemNews.getTitle());
                break;
            case 3:
                log.info("这是通知公告");
                Information information = iInformationService.getById(id);
                cv.setId(information.getId());
                cv.setContent(information.getConten());
                cv.setTitle(information.getTitle());
                break;
        }
        return cv;
    }
}